package xyz.jcat.webmvc.validator;

import xyz.jcat.common.util.ArrayUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class EnumNumValidator implements ConstraintValidator<EnumNum, Integer> {

    private int[] enums;

    @Override
    public void initialize(EnumNum constraintAnnotation) {
        enums = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(Integer num, ConstraintValidatorContext constraintValidatorContext) {
        if(Objects.isNull(num)) {
            return true;
        }
        return ArrayUtils.contains(enums, num);
    }

}
